﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using DIWAGIS.Class;
using DIWAGIS.Modules;
using System.Diagnostics;
using System.ComponentModel;
namespace DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc
{
    public partial class frmDongHoTong : Form
    {
        public frmDongHoTong()
        {
            InitializeComponent();
        }
        public IFeature pFeature;
        Boolean isLoadComBox = false;
        string sDuongDan = "";
        string tbTaiLieu = "TaiLieu_DongHoTong";
        DIWAGIS.Class.general_Model dc = new DIWAGIS.Class.general_Model();

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            IWorkspaceEdit pWorkSpaceEdit;
            pWorkSpaceEdit = generalVariable.ICurrentWorkspace as IWorkspaceEdit;
            pWorkSpaceEdit.StartEditing(true);
            pWorkSpaceEdit.StartEditOperation();

            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.setDataIntoDatatable(this, pFeature);

            pFeature.Store();
            pWorkSpaceEdit.StopEditOperation();
            pWorkSpaceEdit.StopEditing(true);
            pWorkSpaceEdit = null;
            MessageBox.Show("Cập nhật thành công", generalVariable.nameProgram, MessageBoxButtons.OK);
            this.Close();
        }

        private void frmDongHoTong_Load(object sender, EventArgs e)
        {
            DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
            cbx.loadDataDomainToCombobox(CoDongHo, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMCoDongHoTong");

            cbx.loadDataDomainToCombobox(NhaSanXuat, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMNhaSanXuatDongHo");
            cbx.loadDataDomainToCombobox(LoaiCauTao, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMLoaiCauTaoDongHo");
            cbx.loadDataDomainToCombobox(TieuChuan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTieuChuanDongHo");
            cbx.loadDataDomainToCombobox(LoaiMatSo, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMLoaiMatSoDongHo");
            cbx.loadDataDomainToCombobox(TieuChuan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTieuChuanDongHo");
            cbx.loadDataDomainToCombobox(LCCauVanHanh, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMLCCVHDongHo");
            SqlCommand sqlcommand = new SqlCommand();
            //sqlcommand.CommandText = "SELECT TENHUYEN,IDHUYEN FROM " +  generalVariable.database + "." + generalVariable.schema + "." + " HUYEN ";
            sqlcommand.CommandText = "SELECT TENHUYEN,IDHUYEN FROM HUYEN ";
            cbx.loadDataToCombobox(MaQuan, sqlcommand);

            sqlcommand.CommandText = "SELECT TENCONDUONG,IDCONDUONG FROM " + generalVariable.database + "." + generalVariable.schema + "." + " CONDUONG ";
            cbx.loadDataToCombobox(MaDuong, sqlcommand);
            isLoadComBox = true;
            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.loadDataIntoForm(this, pFeature);
            if (pFeature.get_Value(pFeature.Fields.FindField("MaPhuong")) != null)
            {
                MaPhuong.SelectedValue = pFeature.get_Value(pFeature.Fields.FindField("MaPhuong"));
            }

            string query = "";
            query = "SELECT  ID,IDMaDongHo,NgayCapNhat ,TenTaiLieu,LoaiFile FROM  " + tbTaiLieu + " WHERE  IDMaDongHo  = '" + IDMaDongHo.Text + "' ORDER BY NgayCapNhat desc";
            dc.LoadDatatoGridView(query, this.dataGridView1);
        }

        private void MaQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadComBox == true)
            {
                string strWhere = "";
                if (!string.IsNullOrEmpty(MaQuan.Text))
                {
                    strWhere = MaQuan.SelectedValue.ToString();
                    SqlCommand sqlcommand = new SqlCommand();
                    DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
                    //sqlcommand = new System.Data.SqlClient.SqlCommand();
                    sqlcommand.CommandText = "SELECT TenHanhChinh,IDHanhChinh FROM " + generalVariable.database + "." + generalVariable.schema + "." + "HANHCHINH" + " WHERE MaHuyen = @MaHuyen";
                    sqlcommand.Parameters.AddWithValue("@MaHuyen", strWhere);
                    cbx.loadDataToCombobox(MaPhuong, sqlcommand);
                    //loadDataToCombobox(cbxMaPhuong, sqlcommand);

                }
            }
        }

        private void buttonDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Add Image";
            dlg.Multiselect = false;
            dlg.Filter = "Image File (JPEG, GIF, BMG,pdf,word etc.)|" + "*.jpg;*.jpeg;*.gif;*.bmg;*.pdf;*.docx;";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                lblDuongDan.Text = dlg.FileName;
                sDuongDan = dlg.FileName;
            }
            else
            {
                lblDuongDan.Text = "";
            }
        }

        private void cmdXem_Click(object sender, EventArgs e)
        {

            int sNRow = 0;
            DataTable dt = null;
            sNRow = dataGridView1.SelectedRows.Count;
            IMemoryBlobStream pMemoryBlobStream = default(IMemoryBlobStream);
            string sqlWhereAll = null;
            //"SELECT  " & frmFieldName & " ,MaHoSo,TenHoSo,MoTa,LoaiHoSo FROM " & frmTableName & " WHERE  " & frmFieldName & " = '" & _id & "' ORDER BY NgayCapNHat desc"
            string expImgPath = null;

            if (sNRow == 1)
            {
                sqlWhereAll = "SELECT  ID,IDMaDongHo,TenTaiLieu,LoaiFIle,DataBlob FROM " + tbTaiLieu + " WHERE  ID = " + dataGridView1.CurrentRow.Cells[0].Value;

                dt = dc.GetDataTable(sqlWhereAll);

                if (dt.Rows.Count > 0)
                {
                    dc.DeleteFileTemp(dc.CreateDirTemp(AppDomain.CurrentDomain.BaseDirectory + "\\temp") + "\\a." + dt.Rows[0][3].ToString());
                    expImgPath = dc.CreateDirTemp(AppDomain.CurrentDomain.BaseDirectory + "\\temp") + "\\a." + dt.Rows[0][3].ToString();
                    byte[] MyData = null;
                    MyData = (byte[])(dt.Rows[0][4]);
                    int K = 0;
                    K = MyData.GetUpperBound(0);

                    System.IO.FileStream fs = new System.IO.FileStream(expImgPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                    fs.Write(MyData, 0, K);
                    fs.Close();
                    fs = null;

                    Process.Start(expImgPath);

                }
            }
            else if (sNRow == 0)
            {
                MessageBox.Show("Chưa có tài liệu để xem", generalVariable.nameProgram, MessageBoxButtons.OK);
                return;
            }
            else
            {
                MessageBox.Show("Vui lòng chọn tài liệu để xem", generalVariable.nameProgram, MessageBoxButtons.OK);
                return;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            if (sDuongDan == string.Empty)
            {

                MessageBox.Show("Vui lòng chọn đường dẫn đến tài liệu ", generalVariable.nameProgram, MessageBoxButtons.OK);

            }

            else
            {
                try
                {
                    string sQuery = "";
                    sQuery = "INSERT INTO " + tbTaiLieu + "( DataBlob,LoaiFile,IDMaDongHo,TenTaiLieu,NguoiCapNhat,NgayCapNhat)";
                    sQuery = sQuery + "VALUES(@DataBlob,@LoaiFile,@IDMaDongHo,@TenTaiLieu,@NguoiCapNhat,@NgayCapNhat)";


                    SqlCommand sqlcommand = new SqlCommand(sQuery);
                    sqlcommand.Parameters.AddWithValue("@NguoiCapNhat", generalVariable.username);
                    sqlcommand.Parameters.AddWithValue("@NgayCapNhat", DateTime.Now);
                    sqlcommand.Parameters.AddWithValue("@DataBlob", dc.GetByteArray(sDuongDan));
                    sqlcommand.Parameters.AddWithValue("@LoaiFile", dc.getExFileName(sDuongDan));

                    sqlcommand.Parameters.AddWithValue("@TenTaiLieu", sDuongDan);
                    sqlcommand.Parameters.AddWithValue("@IDMaDongHo", IDMaDongHo.Text);
                    dc.ThucThiSQL(sqlcommand);

                    string query = "";
                    query = "SELECT  ID,IDMaDongHo,NgayCapNhat,TenTaiLieu,LoaiFile FROM " + tbTaiLieu + " WHERE  IDMaDongHo  = '" + IDMaDongHo.Text + "' ORDER BY NgayCapNhat desc";
                    dc.LoadDatatoGridView(query, this.dataGridView1);
                    MessageBox.Show("Thêm mới hồ sơ thành công", generalVariable.nameProgram, MessageBoxButtons.OK);
                }

                catch
                {


                }
            }        
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            int sNRow = 0;
            DataTable dt;
            sNRow = dataGridView1.SelectedRows.Count;
            String query = "";
            if (sNRow == 1)
            {
                query = "Delete FROM " + tbTaiLieu + "  WHERE  ID = " + dataGridView1.CurrentRow.Cells[0].Value;
                dt = dc.GetDataTable(query);

                query = "SELECT  ID,IDMaDongHo,NgayCapNhat,TenTaiLieu,LoaiFile FROM  " + tbTaiLieu + " WHERE  IDMaDongHo  = '" + IDMaDongHo.Text + "' ORDER BY NgayCapNhat desc";
                dc.LoadDatatoGridView(query, this.dataGridView1);

                MessageBox.Show("Xóa hồ sơ thành công", generalVariable.nameProgram, MessageBoxButtons.OK);
            }

            else
            {
                MessageBox.Show("Vui lòng chọn tài liệu để xóa", generalVariable.nameProgram, MessageBoxButtons.OK);

            }
        }
        
    }
}
